package com.zemoga.util.youtube
{
	import ca.newcommerce.youtube.events.VideoDataEvent;
	import ca.newcommerce.youtube.events.VideoFeedEvent;
	import ca.newcommerce.youtube.webservice.YouTubeDataClient;
	import ca.newcommerce.youtube.webservice.YouTubeFeedClient;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	
	public class YoutubeSearchVideoProxy extends EventDispatcher
	{
		
		protected var _wsFeed:YouTubeFeedClient;
		protected var _wsData:YouTubeDataClient;
		
		public function YoutubeSearchVideoProxy()
		{
			_wsFeed = YouTubeFeedClient.getInstance();
			_wsData = YouTubeDataClient.getInstance();
			
			initEvents();
		}
		
		public function getVideoByID(videoID:String = "Y_b_-q2kyZU"):void
		{
			_wsFeed.getVideo(videoID);
		}
		
		public function getVideos(videoName:String = "go team"):void
		{
			_wsFeed.getVideos(videoName);
		}
		
		private function initEvents():void
		{
			// triggered by getVideo
			_wsFeed.addEventListener(VideoDataEvent.VIDEO_INFO_RECEIVED, doVideoInfoReady);
			_wsFeed.addEventListener(VideoFeedEvent.VIDEO_DATA_RECEIVED, doVideosReady);
			
			_wsFeed.addEventListener(IOErrorEvent.IO_ERROR, queryError_handler);
		}
		
		protected function doVideoInfoReady(evt:VideoDataEvent):void
		{
			trace("videoInfo", evt.video);
		}
		
		protected function doVideosReady(evt:VideoFeedEvent):void
		{
			trace("video query result", evt.feed);
			var event:YoutubeSearchVideoProxyEvent = new YoutubeSearchVideoProxyEvent(YoutubeSearchVideoProxyEvent.SEARCH_SUCCESS);
			event.data = evt.feed;
			
			dispatchEvent(event);
		}
		
		protected function queryError_handler(event:IOErrorEvent):void
		{
			trace("there is an error",event.toString());
		}
	}
}